<template>
  <el-dialog :visible.sync="visible" width="80%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="160px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="复苏河湖名称" prop="riverId">
            <el-select
              filterable
              size="small" v-model="form.riverId"  placeholder="请选择">
              <el-option
                v-for="item in rvOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="周报名称" prop="reportName">
            <el-input v-model="form.reportName" placeholder="请输入周报名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="引江水流量(m³/s)" prop="divertingFlowRate">
            <el-input type="number" v-model="form.divertingFlowRate" placeholder="请输入引江水流量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="引江水水量(万m³)" prop="divertingAmount">
            <el-input type="number" v-model="form.divertingAmount" placeholder="请输入引江水水量"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="水库水水量(万m³)" prop="reservoirAmount">
            <el-input v-model="form.reservoirAmount" placeholder="请输入水库水水量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="累计补水量(万m³)" prop="grandTotal">
            <el-input type="number" v-model="form.grandTotal" placeholder="请输入累计补水量"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="有水河道长度(km)" prop="withWaterLength">
            <el-input type="number" v-model="form.withWaterLength" placeholder="请输入有水河道长度"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="形成水面面积(km³)" prop="surfaceArea">
            <el-input type="number" v-model="form.surfaceArea" placeholder="请输入形成水面面积"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="日期" prop="date">
            <el-date-picker v-model="form.date" type="date" placeholder="请选择日期" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="期数" prop="periods">
            <el-input type="number" v-model="form.periods" placeholder="请输入期数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="水库水流量(m³/s)" prop="reservoirFlowRate">
            <el-input type="number" v-model="form.reservoirFlowRate" placeholder="请输入水库水流量"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      rvOptions: [],
      form: {
        dictName: '',
        hehuType: []
      },
      hehuOptions: [
        {
          value: 'option1',
          label: '流域1',
          children: [
            {
              value: 'option1-1',
              label: '子流域1-1',
              children: [
                {
                  value: 'option1-1-1',
                  label: '单元1-1-1'
                }
              ]
            }
          ]
        },
        {
          value: 'option2',
          label: '流域2',
          children: [
            {
              value: 'option2-1',
              label: '子流域2-1',
              children: [
                {
                  value: 'option2-1-1',
                  label: '单元2-1-1'
                }
              ]
            }
          ]
        }
      ],
      rules: {
        date: [{ required: true, message: '请输入日期', trigger: 'blur' }],
        divertingAmount: [{ required: true, message: '请输入引江水水量', trigger: 'blur' }],
        divertingFlowRate: [{ required: true, message: '请输入引江水流量', trigger: 'blur' }],
        grandTotal: [{ required: true, message: '请输入累计补水量', trigger: 'blur' }],
        periods: [{ required: true, message: '请输入期数', trigger: 'blur' }],
        reportName: [{ required: true, message: '请输入补水周报名称', trigger: 'blur' }],
        reservoirAmount: [{ required: true, message: '请输入水库水水量', trigger: 'blur' }],
        reservoirFlowRate: [{ required: true, message: '请输入水库水流量', trigger: 'blur' }],
        riverId: [{ required: true, message: '请输入河道名称', trigger: 'blur' }],
        surfaceArea: [{ required: true, message: '请输入形成水面面积', trigger: 'blur' }],
        withWaterLength: [{ required: true, message: '请输入有水河道长度', trigger: 'blur' }],
      },
      type: ''
    };
  },
  methods: {
    getparams(){
      this.$api.getRevivingRiverName({ keywords: ''}).then((res) => {
        if(res && res.code == 200){
          this.rvOptions = res.data || []
        }
      })
    },
    showModal(type, row) {
      this.visible = true
      this.type = type
      this.getparams()
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getRiverReplenishById({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            this.$api.addRiverReplenish(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateRiverReplenish(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
